﻿<?php
if(!defined('IN_PHPSCUP'))exit('hacking'); 

class ViewApp extends BaseApp
{

	var $view_data = array();
	var $view_scup_dir = '';
	var $view_nowconfig_dir = '';

	function __construct() 
	{
      $this->ViewApp();
	}
		
	function ViewApp() 
	{
	   $this->view_scup_dir = ROOT_PATH.'/scup/view'; 
	   $this->view_nowconfig_dir = $this->view_scup_dir.'/'.VIEW_TYPE.'/config'; 	     
	   parent::__construct();
	}	
	


    function index()
    {
        $temparray = array();
        $temparray1 = array();
        $temparray = get_all_dirname($this->view_scup_dir);
        
		foreach($temparray as $key => $value)
		{
		  if(is_file($this->view_scup_dir.'/'.$value.'/config/config.inc'))
		  {
               $this->view_data[$value] = include($this->view_scup_dir.'/'.$value.'/config/config.inc');
               $temparray1 = get_all_dirname($this->view_scup_dir.'/'.$value.'/style');
			   foreach($temparray1 as $key1 => $value1)
			   {
		           if(is_file($this->view_scup_dir.'/'.$value.'/style/'.$value1.'/config.inc'))
		           {
                    $this->view_data[$value]['style'][$value1] = include($this->view_scup_dir.'/'.$value.'/style/'.$value1.'/config.inc');
			   		          		  
		           }				   
			   
			   }
			   if(!isset($this->view_data[$value]['style']))
			   {
			   $this->view_data[$value]['style'] = array();
			   }			   
	          		  
		  }
		  
		}

        $this->assign('infos', $this->view_data);
		    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('view_list.view.php');
    }

    function edit()
    {	

		$id = $_GET['id'];
		$TB_view = &$this->_model('view'); 	 	 
		
		$data = $TB_view->oneshow($id);


	    $this->assign('data', $data);		
			

	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('view_edit.view.html');
    }

    function add()
    {	
	
	

		
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('view_add.view.html');
    }


    function addpost()
    {

	     $data = $_POST['data'];
		 unset($data[path]);
		 
	     include(ROOT_PATH.'/lib/upfile.lib.php');	  
	     $upfile = new Upfile('view'); 	  
	     $updata = $upfile->_batch_upfile('view');		 
		 		 
		 $data[path] = $updata[1][path];		 
	 		 		 

		 $TB_view = &$this->_model('view'); 	 
		
		 $TB_view->post($data);
		 
		 $this->set(array('view_type'=>$data[filename]));		
		 $this->index();

    }
	
    function lstatus()
    {
		 $data=array();  
	     $id = $_GET['id'];
		 $data['view_type'] = $id;
		 $data['css_type'] = 'style1';
		 
         $this->set($data);	 
		 $notice = &get_message(array('msg'=>"修改成功!",'url'=>'?app=view&act=index','title'=>'Notice'));
		 $notice ->display();	
    }

    function setcss()
    {
		 $data=array();  
	     $css = $_GET['v'];
	     $view = $_GET['view'];	
         if(empty($css)||empty($view))
		 {
		 $notice = &get_message(array('msg'=>"参数有错!",'url'=>'?app=view&act=index','title'=>'Notice'));
		 $notice ->display();		 
		 }		 
         if($view!=VIEW_TYPE)
		 {
		 $notice = &get_message(array('msg'=>"你选择的不是本模板的样式!",'url'=>'?app=view&act=index','title'=>'Notice'));
		 $notice ->display();		 
		 }		 	 
		 $data['css_type'] = $css;
		 
         $this->set($data);	 
		 $notice = &get_message(array('msg'=>"修改成功!",'url'=>'?app=view&act=index','title'=>'Notice'));
		 $notice ->display();	
    }

    function editpost()
    {

	     $data = $_POST['data'];
		 unset($data[path]);
		 
		 $id = intval($data['id']);	 
		 $TB_view = &$this->_model('view');  		 		 		
		 $picdata = $TB_view->oneshow($id);
		 
		 
	     include(ROOT_PATH.'/lib/upfile.lib.php');	  
	     $upfile = new Upfile('view'); 	  
	     $updata = $upfile->_batch_upfile('view');
		 
		 if($picdata['images_dir'] && $updata)
		 {
		 $pic_dir = ROOT_PATH . '/' . UPFILE_PATH . '/' . $picdata['images_dir'];
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		                del_file($pic_dir);	
		              }				 
		 }
		 		 

		 
		 if(empty($updata))
		 {
		 $data[path] = $picdata['images_dir'];		 
		 }
		 else
		 {
		 $data[path] = $updata[1][path];		 
		 }
		
		 $TB_view->editaction($id,$data);
		 

		 $this->set(array('view_type'=>$data['filename']));		 
		 $this->index();
		 

    }




    function set($data)
    {

		$TB_set = &$this->_model('setting'); 	
	
		$setdata = $TB_set->show();

		$temp_setdata = array();

		
		foreach($setdata as $key => $value)
		{
           $temp_setdata[$value['keyname']] = $value['value'];		   
		}

		
		foreach($data as $key => $value)
		{
           $temp_setdata[$key] = $value;	   
		}
 
		$TB_set->replace('keyname','value',$temp_setdata);
		  
 		$this->write_config($temp_setdata);
		 
     
    }

		


    function write_config($data)
    {

		 $str = '';
		 
         foreach ($data as $key => $value)
         {
               if (is_string($value) || is_numeric($value) || is_bool($value) || is_null($value))
               {
                    $str .= "define('".strtoupper($key)."','".$value."');\r\n";

                }
         }
		 		 	    	
		 write_file(ROOT_PATH . '/data/config.inc.php',"<?php\r\n".$str.'?>');

    }



  
}

?>